﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

using Newtonsoft.Json.Linq;

namespace KorbeRTR.MES
{
    public class AlarmHelper
    {
        [Alarm("VCP-BJ-0001", "上板超时")]
        public bool SB上板超时;

        [Alarm("VCP-BJ-0002", "周边温度未开启")]
        public bool ZB周边温度未开启;

        private readonly Dictionary<string, bool> LastAlarmDict = new Dictionary<string, bool>();

        public void ScanAlarm()
        {
            var fields = typeof(AlarmAttribute).GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
            foreach (var iter in fields)
            {
                var attr = iter.GetCustomAttribute<AlarmAttribute>();
                if (attr == null)
                {
                    continue;
                }

                bool isAlarm = Convert.ToBoolean(iter.GetValue(this));

                LastAlarmDict[attr.ID] = isAlarm;
            }
        }

        /// <summary>
        /// 获取报警变化的Json
        /// </summary>
        public JArray ToJson()
        {
            var array = new JArray();

            var fields = typeof(AlarmAttribute).GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
            foreach (var iter in fields)
            {
                var attr = iter.GetCustomAttribute<AlarmAttribute>();
                if (attr == null)
                {
                    continue;
                }

                bool current = Convert.ToBoolean(iter.GetValue(this));
                bool last = LastAlarmDict[attr.ID];

                if (current == last)
                {   // 报警无变化
                    continue;
                }

                var obj = new JObject
                {
                    ["ID"] = attr.ID,
                    ["Name"] = attr.Name,
                    ["Describe"] = attr.Describe,
                    ["isAlarm"] = current,
                };

                array.Add(obj);
            }

            return array;
        }

        /// <summary>
        /// 获取全部Json
        /// </summary>
        public JArray AllToJson()
        {
            var array = new JArray();

            var fields = typeof(AlarmAttribute).GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
            foreach (var iter in fields)
            {
                var attr = iter.GetCustomAttribute<AlarmAttribute>();
                if (attr == null)
                {
                    continue;
                }

                bool current = Convert.ToBoolean(iter.GetValue(this));

                var obj = new JObject
                {
                    ["ID"] = attr.ID,
                    ["Name"] = attr.Name,
                    ["Describe"] = attr.Describe,
                    ["isAlarm"] = current,
                };

                array.Add(obj);
            }

            return array;
        }

        /// <summary>
        /// 获取注册表
        /// </summary>
        public static JArray GetRegistry()
        {
            var array = new JArray();

            var fields = typeof(AlarmAttribute).GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
            foreach (var iter in fields)
            {
                var attr = iter.GetCustomAttribute<AlarmAttribute>();
                if (attr == null)
                {
                    continue;
                }

                var obj = new JObject
                {
                    ["ID"] = attr.ID,
                    ["Name"] = attr.Name,
                    ["Describe"] = attr.Describe
                };

                array.Add(obj);
            }

            return array;
        }
    }

    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
    public class AlarmAttribute : Attribute
    {
        public AlarmAttribute(string Id, string Name, string Describe = "")
        {
            this.ID = Id;
            this.Name = Name;
            this.Describe = Describe;
        }

        public string ID;

        public string Name;

        public string Describe;
    }
}
